package login;

import io.ClientSession;
import io.OperationResponse;

import java.util.HashMap;

import org.codehaus.jettison.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import platform.AppConfig;
import protocol.ParameterType;
import protocol.ReturnCode;

/**
 * 閼辨棁绻峴dk缁夌粯顦�
 * 
 * @author zhengjiangyu
 * 
 */
public class ZsyAllLogin {
	protected static final Logger log = LoggerFactory.getLogger(ZsyAllLogin.class);

	static String zsyALLGameID;
	static String zsyALLServerID;
	static String zsyALLApp_key;
	static String zsyALLSecret_key;
	static String zsyALLSyn_key;
	static String zsyALLProjectID;

	static String zsyALLServerInfoUrl;
	static String zsyALLServerUrl;

	public static void LoginZSYALL(OperationResponse operationRequest,
			ClientSession session) {

		try {
			if (operationRequest.Parameters.size() < 2) {

				OperationResponse response = new OperationResponse(
						operationRequest.OperationCode, null, ReturnCode.No, "");
				session.SendOperationResponse(response);
			} else {
				String id = (String) operationRequest.Parameters
						.get(ParameterType.UserID);
				String token = (String) operationRequest.Parameters
						.get(ParameterType.UserPwd);
				// String databeas = (String) operationRequest.Parameters
				// .get(ParameterType.DataBeas);
				// String table = (String) operationRequest.Parameters
				// .get(ParameterType.TableName);
				String isGetIP = (String) operationRequest.Parameters
						.get(ParameterType.IsGetIP);
				String deviceID = (String) operationRequest.Parameters
						.get(ParameterType.DeviceID);
				String sdk = (String) operationRequest.Parameters
						.get(ParameterType.SDK);
				String PhoneType=(String) operationRequest.Parameters
						.get(ParameterType.PhoneType);
				
				HashMap<String, Object> dicTempInfo = null;
				if (AppConfig.dicZSYAllInfo.containsKey(sdk)) {
					dicTempInfo = AppConfig.dicZSYAllInfo.get(sdk);
					zsyALLGameID = (String) dicTempInfo.get("GameID");
					zsyALLServerID = (String) dicTempInfo.get("ServerID");
					zsyALLApp_key = (String) dicTempInfo.get("App_key");
					zsyALLSecret_key = (String) dicTempInfo.get("Secret_key");
					zsyALLSyn_key = (String) dicTempInfo.get("Syn_key");
					zsyALLProjectID = (String) dicTempInfo.get("ProjectID");
					zsyALLServerInfoUrl = (String) dicTempInfo
							.get("ServerInfoUrl");
					zsyALLServerUrl = (String) dicTempInfo.get("ServerUrl");

				} else {
					return;
				}
				if (id.equals("zealm") && token.equals("")) {
					OperationResponse response = new OperationResponse(
							operationRequest.OperationCode,
							AppConfig.dicAppInfo, ReturnCode.GetServer, "");
					session.SendOperationResponse(response);
					return;

				}
				HashMap<String, String> dicBack = GetLoginZSYAll(id);
				if (dicBack == null) {
					return;

				}
				id = sdk + dicBack.get("sdkuserid");
				String zsyBack = String.format("%1$s;%2$s;%3$s;%4$s;%5$s",
						dicBack.get("authCode"), dicBack.get("username"),
						dicBack.get("sdkuserid"), dicBack.get("access_token"),
						dicBack.get("id"));
				HashMap<Short, Object> parmBack = new HashMap<Short, Object>();
				parmBack.put(ParameterType.ItemID, zsyBack);
				OperationResponse response = new OperationResponse(
						operationRequest.OperationCode, parmBack,
						ReturnCode.ZSYBack, "");
				session.SendOperationResponse(response);
				operationRequest.Parameters.put(ParameterType.UserID, id);
				HashMap<Short, Object> myPrams = new HashMap<Short, Object>();
				myPrams.put(ParameterType.UserID, id);
				myPrams.put(ParameterType.ItemID, zsyBack);
				OperationResponse responseID= new OperationResponse(
						operationRequest.OperationCode, myPrams,
						ReturnCode.HasID, "");
				session.SendOperationResponse(responseID);
			Util.sdkReceived(operationRequest, session,id, deviceID, isGetIP,"lianyun",PhoneType,"");

			}


		} catch (Exception e) {
			log.debug(e.toString());
		}

	}

	private static HashMap<String, String> GetLoginZSYAll(String mCode)
			throws Exception {
		if (mCode.isEmpty()) {
			return null;

		}

		HashMap<String, String> param = new HashMap<String, String>();
	
		String body="client_secret="+zsyALLSecret_key+"&"+"grant_type=authorization_code&redirect_uri=1&code="+mCode+"&client_id="+zsyALLApp_key+"&productId="+zsyALLGameID;

		
		String result = Util.sendPost(zsyALLServerUrl, body);
		result = new String(result.getBytes("gbk"), "utf-8");
		JSONObject token = new JSONObject(result);
		String access_token = token.optString("access_token");
		String code = token.optString("codes");
		if (!access_token.equals("") && code.equals("0")) {
			body="access_token="+access_token;
			result=Util.sendPost(zsyALLServerInfoUrl, body);
			JSONObject token1=new JSONObject(result);
			JSONObject data=token1.getJSONObject("data");
			String username=data.optString("username");

			String sdkuserid=data.optString("sdkuserid");

			String id=data.optString("id");
			param.put("authCode", mCode);
			param.put("username", username);
			param.put("sdkuserid", sdkuserid);
			param.put("id", id);
			param.put("access_token", access_token);

			return param;
		}

		return null;

	}
}
